package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)//提交
    public User findUser(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<User> findAll(User user) {
        PageHelper.startPage(1,2);
        return userDao.findAllUsers(user);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<User> findAll(PageVo pageVo,User user) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> users = userDao.findAllUsers(user);
        return new PageInfo<User>(users);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//开启事务回调函数
    public Integer addUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delUser(Integer[] ids) {
        return userDao.deleteUser(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUser(User user) {
        return userDao.updateUser(user);
    }
}
